import  requests
from bs4 import BeautifulSoup
import csv
def get_html(url):
    try:
        r=requests.get(url)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as error:
        print(error)
def parser(html):
    soup=BeautifulSoup(html,"lxml")
    out_list=[]
    for i in soup.select("#pane-news ul>li"):
        row_list=[i.text.replace(',','').replace("\n",''),
                  i.select("a")[0].attrs["href"].replace(",","").replace("\n","").split()]
        out_list.append(row_list)
    return  out_list
def save_csv(itme,path):
    with open(path,"a+",newline='',encoding='utf-8')as f:
        csv_write=csv.writer(f)
        csv_write.writerows(itme)
if __name__ == '__main__':
    url = "http://www.bspider.top/baidunews/"
    html = get_html(url)
    out_list = parser(html)
    save_csv(out_list,'news.csv')